package com.fairytask.ui
{
	import com.fairytask.data.Modulator;
	import com.fairytask.data.ViewObject;
	import com.fairytask.engine.GameStage;
	import com.fairytask.net.CustomToClientEvent;
	import com.fairytask.ui.base.FrameUI;
	import com.fairytask.ui.item.BuffItem;
	
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * buff系统显示
	 * @author yangj
	 **/
	
	
	public class BuffUI extends FrameUI
	{
		private static var _self			:BuffUI;
		
		private var _sp :Sprite	=	new Sprite();					//用于显示buff的容器
		private var _buffArr		:Array = new Array();				//用于存放需要显示的对象 
		private var num			:int	=	4;
		private var _arr			:Array	=	[];						//用于存放绑定视图的属性，当删除的时候进行解绑
		private var _model		:Modulator = Modulator.instance;
		
		//用于获得到单例的对象
		public static function getInstance():BuffUI
		{
			//如果已经有这个对象了，就获取到，如果没有就创建这个实例
			if(_self == null)
			{
				_self	=	new BuffUI();				
			}
			return _self;
		}
		
		public function BuffUI(popUI:Boolean=false)
		{
			super(popUI);
			initX = this.x;
			initY = this.y;
			addChild(_sp);
			initBind();
		}
		
		/**
		 * 初始化数据绑定 
		 * 
		 */
		private function initBind():void
		{
			/*bindManager.BindView(model.scene,ViewDefine.VIEWPORT_OBJ_STATUS,onAddBuff,onRemoveBuff);
			_model.addEventListener(CustomToClientEvent.SERVER_CUSTOMMSG_ADD_NEW_BUFFER,addFilter);*/
		}
		
		private function addFilter(evt:CustomToClientEvent):void
		{
			/*for(var i:int=0;i<_buffArr.length;i++)
			{
				if((_buffArr[i] as BuffItem).bufferID ==evt.pList[0])
				{
					(_buffArr[i] as BuffItem).addFilter();
				}	
			}*/
		}
		
		//添加buff
		private function onAddBuff(key:String,val:ViewObject,pList:Array):void
		{
			if(val is ViewObject)
			{
				//如果以前已经显示的就不再显示
				for(var i:int=0;i<_buffArr.length;i++)
				{
					if((_buffArr[i] as BuffItem).data.ObjKey == key)
					{
						return;
					}
				}
				var tmpBuff		:BuffItem	=	new BuffItem();
				tmpBuff.data				=	val;
				_buffArr.push(tmpBuff);
				//_arr[int(key)]	=	[val,"LifeTime",bindManager.BindPropInViewObj(model.scene.GetView(ViewDefine.VIEWPORT_OBJ_STATUS),key,"LifeTime",addTime,null,[tmpBuff])];

			}
			refresh();
		}
		/**
		 *当时间改变的的时候进行的操作 
		 * 
		 */
		private function addTime(key:String,value:int,pList:Array):void
		{
			var tmpBuff		:BuffItem	=	pList[0];
			tmpBuff.lifeTime			=	value;

		}
		
		 //buff被移除  
		private function onRemoveBuff(key:String,val:ViewObject,pList:Array=null):void
		{
			if(val is ViewObject)
			{
				var itemArr:Array	=	[];
				itemArr	=	_arr[int(key)];
				bindManager.UnbindProp(itemArr[0],itemArr[1],itemArr[2]);
				
				for(var i:int=0;i<_buffArr.length;i++)
				{
					if((_buffArr[i] as BuffItem).data.ObjKey == key)
					{
						_buffArr.splice(i,1);
						break;
					}
				}
			}

			refresh();
		}
		
		//重新显示
		private function refresh():void
		{
			while(_sp.numChildren>0)
			{
				_sp.removeChildAt(0);
			}
			if(_buffArr.length==0)
			{
				return;
				
			}
			var line:int	=	_buffArr.length/num==0?_buffArr.length/num:_buffArr.length/num+1;
			var i:int		=	0;	
				for(var j:int=0;j<line;j++)
				{
					for(var z:int=0;z<num;z++)
					{
						if(_buffArr.length<=i)
						{
							return;
						}
						var tmpBuff		:BuffItem	=	_buffArr[i] as BuffItem;
						tmpBuff.x	=	j*24+2;
						tmpBuff.y	=	z*24+2;
						tmpBuff.width	=	24;
						tmpBuff.height	=	24;
						_sp.addChild(tmpBuff);
						i++;
					}
					
				}
			
		}
		
		override public function show(space:String=BACKLAYER, onshow:Function=null):void
		{
			super.show(space,onshow);
		}
		
		/**
		 * 场景变化改变位置 
		 * 
		 */
		override protected function stageReSize(evt:Event=null):void
		{
			this.x	=	initX - GameStage.mainLayer.x;
			this.y = 	initY - GameStage.mainLayer.y;
		}
	}
}

